/**
 *
 * User: rendong
 * Date: 12-9-4
 * Time: 下午9:53
 * $Id: GDisplayObjectContainer.js 11 2012-09-06 15:55:53Z rendong237@gmail.com $
 */

function GDisplayObjectContainer(){}

GDisplayObjectContainer.extend(new GDisplayObject());

GDisplayObjectContainer.prototype.mouseChildren = true;

GDisplayObjectContainer.prototype.numChild = function(){
    return this._childs.length;
}

/**
 * 子对象数组
 * @type {Array}
 * @private
 */
GDisplayObjectContainer.prototype._childs = [];


/**
 * 添加显示对象
 * @param child {GDisplayObject}
 */
GDisplayObjectContainer.prototype.addChild = function(child){
    if (child == null)
        throw new Error("child is null!");

    if (child.parent != null)
        throw new Error("child parent is not null");

    if (!child instanceof GDisplayObject)
        throw new Error("child no instanceof GDisplayObject");

    child.parent = this;
    child.stage = this.stage;

    this._childs.push(child);

    if (child.stage != null)
        child.dispatchEventByType(GEvent.ADDED_TO_STAGE);
}

GDisplayObjectContainer.prototype.addChildAt = function(child, index){
    if (child == null)
        throw new Error("child is null!");

    if (child.parent != null)
        throw new Error("child parent is not null");

    if (!child instanceof GDisplayObject)
        throw new Error("child no instanceof GDisplayObject");

    if (index < 0 || index >= this._childs.length)
        throw new Error("child index not found! index="+index);

    child.parent = this;
    child.stage = this.stage;

    this._childs.splice(index, 0, child);

    if (child.stage != null)
        child.dispatchEventByType(GEvent.ADDED_TO_STAGE);

    // TODO:判断child类型，如果为GDisplayObjectContainer类型
}


/**
 * 移除显示对象
 * @param child
 */
GDisplayObjectContainer.prototype.removeChild = function(child){
    for(var i= 0, len = this._childs.length; i < len; i++){
        if (this._childs[i] == child){
            this._childs.splice(i, 1);
            child.parent = null;
            child.stage = null;
            return ;
        }
    }
}


/**
 * 根据索引移除显示对象
 * @param index
 */
GDisplayObjectContainer.prototype.removeChildAt = function(index){
    if (i >=0 || i < this._childs.length){
        var child = this._childs[index];
        child.parent = null;
        child.stage = null;

        this._childs.splice(index, 1);
    }
}


/**
 * 判断是否包含该显示对象
 * @param child
 * @return Boolean
 */
GDisplayObjectContainer.prototype.contains = function(child){
    for(var i= 0, len=this._childs.length; i< len; i++){
        if (this._childs[i] == child)
            return true;
    }

    return false;
}

/**
 * 根据索引获取显示对象
 * @param index
 * @return {GDisplayObject}
 */
GDisplayObjectContainer.prototype.getChildAt = function(index){
    if (index <0 || index >= this._childs.length)
        throw new Error("index not found! index="+ index);

    var disp = this._childs[index];

    return disp;
}

/**
 * 获取显示对象索引
 * @param child
 */
GDisplayObjectContainer.prototype.getChildIndex = function(child){
    var index = -1;

    for(var i= 0, len=this._childs.length; i< len; i++){
        if (this._childs[i] == child){
            index = i;
            break;
        }
    }

    if (index == -1)
        throw new Error("child not found, index=-1");

    return index;
}


/**
 * 设置显示对象索引
 * @param child
 * @param index
 */
GDisplayObjectContainer.prototype.setChildIndex = function(child, index){
    if (child == null)
        throw new Error("child is null!");

    if (index < 0 || index >= this._childs.length)
        throw new Error("child index not found! index="+index);

    var oldindex = this.getChildIndex(child);
    if (oldindex == index)
        return;

    this._childs.splice(index, 0, child);
    this._childs.splice(oldindex, 1);
}

